/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2015-9-18 17:18:21
 ******************************************************************************/
package cn.com.xy.sms.sdk.net;

import java.io.ByteArrayInputStream;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.log.LogManager;

public class Https {
    private SSLContext sslContext;
    private SSLSocketFactory sslSocketFactory;
    private HostnameVerifier hostnameVerifier;
    private static Https _default = null;
    private static Https _default1 = null;
    private int mType = 1;

    public static Https getInstance(int type) throws Exception {
        synchronized (Https.class) {
            if (type == 0) {
                if (_default1 == null) {
                    _default1 = new Https(type);
                }
                return _default1;
            } else {
                if (_default == null) {
                    _default = new Https(type);
                }
                return _default;
            }
        }
    }

    private Https(int type) throws Exception {
        this.mType = type;
        this.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                try {
                    LogManager.d("Https", "hostName: ", hostname);
                    if (mType == 0) {
                        if (hostname != null
                                && (hostname.indexOf("duoqu.in") != -1 || hostname
                                        .indexOf("bizport.cn") != -1)) {
                            return true;
                        }
                    }
                    HostnameVerifier hv = HttpsURLConnection
                            .getDefaultHostnameVerifier();
                    if (hv == null) {
                        return false;
                    }
                    return hv.verify(hostname, session);
                } catch (Throwable e) {
//                    e.printStackTrace();
                    LogManager.e(Constant.TAG, "verify: ", e);

                }
                return false;
            }

        });
    }

    public synchronized SSLSocketFactory getSslSocketFactory() throws Exception {

        if (this.sslSocketFactory == null) {
            SSLSocketFactory sslSf = getSSLContext().getSocketFactory();
            this.sslSocketFactory = sslSf;
        }
        return this.sslSocketFactory;
    }

    public synchronized SSLContext getSSLContext() throws Exception {
        if (this.sslContext == null) {
            String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
            TrustManagerFactory tmf = TrustManagerFactory
                    .getInstance(tmfAlgorithm);
            tmf.init((KeyStore) null);
            SSLContext ctx = SSLContext.getInstance("TLS");
            ctx.init(null, tmf.getTrustManagers(), new SecureRandom());
            this.sslContext = ctx;
        }
        return this.sslContext;
    }

    // public static synchronized SSLContext getDefaultSSLContext(int type)
    // throws Throwable {
    // return Https.getInstance(type).getSSLContext();
    // }

    public static X509Certificate importX509Cert(String text) throws Exception {
        return (X509Certificate) importCert(text, "X.509");
    }

    public static Certificate importCert(String text, String type)
            throws Exception {
        return CertificateFactory.getInstance(type).generateCertificate(
                new ByteArrayInputStream(text.getBytes()));
    }

    public HostnameVerifier getHostnameVerifier() {
        return hostnameVerifier;
    }

    public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
        this.hostnameVerifier = hostnameVerifier;
    }
}
